﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Data;

namespace asp_mysql
{
    public class conexion
    {
        MySqlConnection con = new MySqlConnection("server = localhost; user id = root; password = software691; database = controlescolar");
        public MySqlCommand comando;


        MySqlDataReader lector;

        public void abrir()
        {
            try
            {

                con.Open();
            }
            catch (Exception ex)
            {

                //MessageBox.Show(ex.Message);
            }
        }

        public void cerrar()
        {

            con.Close();
        }

        public bool acceso(string cadena)
        {
            comando = new MySqlCommand(cadena, con);//va a tener la consulta cadena del conector
            lector = comando.ExecuteReader();//aqui se van agregar los datos

            if (lector.Read())//si leyo algo 
            {
                return true;
            }
            else//sino encontro nada
            {
                return false;
            }
        }

        public bool grabar(string cadena)
        {
            try
            {
                comando = new MySqlCommand(cadena, con);
                comando.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool usuario(string query)
        {
            con.Open();
            comando = new MySqlCommand(query, con);
            MySqlDataReader lector;
            lector = comando.ExecuteReader();

            if (lector.Read())
            {
                con.Close();
                return true;
            }

            else
            {
                con.Close();
                return false;
            }
            
        }


        public DataTable cargar(string query)
        {
            DataTable tabla = new DataTable();
            MySqlDataAdapter da;

            try
            {
                con.Open();
                da = new MySqlDataAdapter(query, con);
                da.Fill(tabla);
                con.Close();
                return tabla;
            }

            catch (Exception ex)
            {
                con.Close();
                return tabla;
            }

        }

        //public bool grabar(string cadena)
        //{
        //    try
        //    {
        //        comando = new MySqlCommand(cadena, con);
        //        comando.ExecuteNonQuery();
        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        return false;
        //    }
        //}
    }
}